package org.example.game.history;

import java.util.Arrays;
import java.util.HashMap;

/**
 * @Description: TODO
 * @Author wyatt
 * @Data 2024/06/16 11:05
 */
public class Solution100316 {

    public static void main(String[] args) {
        Solution100316 solution100316 = new Solution100316();
        System.out.println(solution100316.maximumTotalDamage(new int[]{3,2,2,1,1,2,2,4,1,1}));
    }

    public long maximumTotalDamage(int[] power) {


        HashMap<Integer, Long> countMap = new HashMap<>();
        for (int i = 0; i < power.length; i++) {
            countMap.putIfAbsent(power[i], 0L);
            countMap.put(power[i], countMap.get(power[i]) + 1);
        }

        Integer[] nums =  countMap.keySet().toArray(new Integer[0]);
        Arrays.sort(nums);
        Long[] dp = new Long[nums.length];
        dp[0] = nums[0] * countMap.get(nums[0]);
        Long sum = dp[0];
        for (int i = 1; i < nums.length; i++) {
            Long num = nums[i] * countMap.get(nums[i]);
            if(nums[i] - nums[i-1] > 2){
                dp[i] = dp[i-1] + num;
            }else if(i == 1){
                dp[i] = Math.max(num, dp[i-1]);
            }else if(nums[i] - nums[i-2] > 2){
                Long num1 = nums[i-1] * countMap.get(nums[i-1]);
                dp[i] = dp[i-2] + Math.max(num1, num);
            }else {
                Long num1 = nums[i-1] * countMap.get(nums[i-1]);
                Long num2 = nums[i-2] * countMap.get(nums[i-2]);
                dp[i] = Math.max(Math.max(num1, num), num2);
                if(i>=3){
                    dp[i] += dp[i-3];
                }
            }
            sum = Math.max(dp[i], sum);
        }
        return sum;
    }

}
